package com.svms.save.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.svms.save.entitys.SvdaWzxx;
import com.svms.save.entitys.SvdaYhb;
import com.svms.save.services.SvdaWzxxService;
import com.svms.save.services.SvdaYhService;
import com.svms.save.utils.SysCode;
import com.svms.save.vo.LayuiTableResult;
import com.svms.save.vo.Page;
import com.svms.save.vo.ResultVo;

@Controller
@RequestMapping(value = "/wzxx")
public class SvdaWzxxController {
	private Logger log = LoggerFactory.getLogger(SvdaWzxxController.class);
	@Autowired
	private SvdaWzxxService wzxxManager;
	@Autowired
	private SvdaYhService yhManager;
	
	@RequestMapping(value = "/toListPage",method = RequestMethod.GET)
	public String toListPage(Model model,HttpServletRequest request) {
		return "wzxx/listPage";
	}
	@RequestMapping(value = "/query")
	@ResponseBody
	public LayuiTableResult<List<SvdaWzxx>> queryListPage(Model model,Page<SvdaWzxx> page,SvdaWzxx wzxx){
		LayuiTableResult<List<SvdaWzxx>> res = null;
        String code = "";
        try {
            page = wzxxManager.queryPage(page,wzxx);
            code = ResultVo.SUCCESSS;
        }catch(Exception e) {
            log.error("系统异常！",e);
            code = ResultVo.SYSTEM_ERROR;
        }
        res = LayuiTableResult.createTableResult(page, code, "");
        return res;
	}
	
	@RequestMapping(value = "/edit",method = RequestMethod.GET)
	public String toEdit(Model model,String objectid) {
		model.addAttribute("objectid", objectid);
		return "wzxx/edit";
	}
	@RequestMapping(value = "/detailByOid")
	@ResponseBody
	public ResultVo<SvdaWzxx> detail(Model model,Long objectid){
		ResultVo<SvdaWzxx> res = null;
		try {
			SvdaWzxx detail = wzxxManager.findById(objectid);
			res = ResultVo.success(SysCode.MSG_SAVE_OK, detail);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.error("系统异常！",e);
			res = ResultVo.systemError(SysCode.MSG_SAVE_ERR, "");
		}
		return res;
	}
	
	@RequestMapping(value = "/doSave",method = RequestMethod.POST)
	@ResponseBody
	public ResultVo<String> doSave(Model model,HttpServletRequest request,SvdaWzxx wzxx){
		ResultVo<String> res = null;
		SvdaYhb yh = yhManager.getCurrentUser(request);
		try {
			int r = wzxxManager.doSave(wzxx, yh);
			if(r > 0) {
				res = ResultVo.success(SysCode.MSG_SAVE_OK, "");
			}else {
				res = ResultVo.businessError(SysCode.MSG_SAVE_ERR, "");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.error("系统异常！",e);
			res = ResultVo.systemError(SysCode.MSG_SAVE_ERR, "");
		}
		return res;
	}
	@RequestMapping(value = "/valWzbh",method = RequestMethod.POST)
	@ResponseBody
	public ResultVo<String> validateWzbh(Model model,String wzbh){
		ResultVo<String> res = null;
		try {
			SvdaWzxx flag = wzxxManager.validateWzbh(wzbh);
			if(null == flag) {
				res = ResultVo.success("", "");
			}else {
				res = ResultVo.businessError(SysCode.BUS_MSG_WZBHCF, "");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.error("系统异常！",e);
			res = ResultVo.systemError(SysCode.MSG_SAVE_ERR, "");
		}
		return res;
	}
}
